﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringFormattingEncoding
{
    class Fraction: IFormattable
    {
        private int numerator;
        private int denominator;

        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }

        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                if (value != 0)
                {
                    denominator = value;
                }
                else
                {
                    throw new ArgumentException("Denominator cannot be zero.");
                }
            }
        }

        public Fraction(int numerator, int denominator)
        {
            this.Numerator = numerator;
            this.Denominator = denominator;
        }

        public override string ToString()
        {
            double result = ((double)this.Numerator)/this.Denominator;
            string resultstr = result.ToString();
            return (resultstr);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case "N":
                    return (string.Format("{0}/{1}", this.Numerator, this.Denominator));
                case "R":
                    return (string.Format("{0:0.00}", (((double)this.Numerator) / this.Denominator)));
                case null:
                    return this.ToString();
                default:
                    throw new FormatException("String format was in incorrect format");
            }
        }
    }
}
